let http = require('https')
let fs = require('fs')
let cheerio = require('cheerio')
let request = require('request')

//初始url
// let urls = ['https://www.youyoubai.com/index.php/2020/03/18/influxdb1-7%e7%89%88%e5%ae%89%e8%a3%85%e4%b8%8e%e9%85%8d%e7%bd%aewindows%e7%8e%af%e5%a2%83%e3%80%81jmeterinfluxdbgrafana%e6%80%a7%e8%83%bd%e7%9b%91%e6%8e%a7/']
let urls = ['https://www.jianshu.com/p/cc01443d1359']
let url = ""

let start = async function () {
  for (let i = 0; i < urls.length; i++) {
    url = urls[i]
    await startRequest(url)
  }
}

function startRequest(x) {
  //采用http模块向服务器发起一次get请求      
  http.get(x, function (res) {
    //用来存储请求网页的整个html内容
    let html = ''
    //防止中文乱码
    res.setEncoding('utf-8')
    //监听data事件，每次取一块数据
    res.on('data', function (chunk) {
      html += chunk
    });
    //监听end事件，如果整个网页内容的html都获取完毕，就执行回调函数
    res.on('end', function () {
      //采用cheerio模块解析html
      let $ = cheerio.load(html)
      let imgurl = []

      let box = $('#app li')
      box.each((index, item) => {
        let img = $(item).find('img').attr('src')
        let name = $(item).find('h6').text()
        imgurl.push({
          img,name
        })
      })
      
      // let name = $('.main > .container .post-bar .meta.text-center > h1').text().trim()
      // savedImg($, name)
    });
  }).on('error', function (err) {
    console.log(err)
  });
}

async function savedImg($, name) {
  $('html body main div article img').each(function (index, item) {
    let img_filename = index + '.jpg'
    //获取图片的url
    let img_src = item.attribs.src

    if (img_src.length > 100) {
      let path = './image/' + name + '---' + img_filename

      //去掉图片base64码前面部分data:image/png;base64
      let base64 = img_src.replace(/^data:image\/\w+;base64,/, "")

      //把base64码转成buffer对象
      let dataBuffer = Buffer(base64, 'base64')
      console.log('dataBuffer是否是Buffer对象：' + Buffer.isBuffer(dataBuffer))

      //用fs写入文件
      fs.writeFile(path, dataBuffer, function (err) {
        if (err) {
          console.log(err)
        } else {
          console.log(index)
          console.log('写入成功！')
        }
      })
    } else {
      //采用request模块，向服务器发起一次请求，获取图片资源
      //通过流的方式，把图片写到本地/image目录下，并用标题和图片的标题作为图片的名称。
      request(img_src).pipe(fs.createWriteStream('./imageDownload/' + name + '---' + img_filename))

      console.log(index)
      console.log('写入成功！')
    }
  })
}

start()